package com.credithc.eagleeye.console.service.impl;
import com.credithc.eagleeye.console.factory.ESFactory;
import com.credithc.eagleeye.console.service.SettingService;
import com.credithc.eagleeye.console.vo.PlatformVo;
import com.credithc.eagleeye.console.vo.SettingVo;
import com.credithc.eagleeye.console.vo.UserVo;
import com.credithc.eagleeye.util.FastJsonConvert;
import com.credithc.framework.es.util.ElasticSearchUtil;
import com.credithc.framework.es.vo.DocumentVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2018年10月16日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Service
@Slf4j
public class SettingServiceImpl implements SettingService {
    private static final String SETTING_INDEX = ".eagleeye-setting";
    private static final String SETTING_TYPE = "setting";
    private static final String SETTING_TYPE_ID = "setting";
    private static final String PLATFORM_TYPE = "platform";
    private static final String USER_TYPE = "user";

    @Override
    public List<PlatformVo> loadAllPlatform() {
        ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        List<PlatformVo> platformVoList = elasticSearchUtil.search(PlatformVo.class, SETTING_INDEX, PLATFORM_TYPE, boolQueryBuilder,null,1024);
        return platformVoList;
    }
    @Override
    public PlatformVo loadPlatform(String platformName) {
        PlatformVo platformVo = new PlatformVo();
        ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("_id", platformName));
        List<PlatformVo> platformVoList = elasticSearchUtil.search(PlatformVo.class, SETTING_INDEX, PLATFORM_TYPE, boolQueryBuilder);
        if (platformVoList != null && platformVoList.size() > 0) {
            platformVo = platformVoList.get(0);
        }
        return platformVo;
    }
    @Override
    public void savePlatformList(List<PlatformVo> platformVoList) {
        if(platformVoList!=null&&platformVoList.size()>0) {
            ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
            //删除旧的
            List<PlatformVo> oldPlatformVoList = loadAllPlatform();
            if(oldPlatformVoList!=null&&oldPlatformVoList.size()>0) {
                List<String> idList = new ArrayList<>();
                for(PlatformVo platformVo : oldPlatformVoList) {
                    idList.add(platformVo.getName());
                }
                elasticSearchUtil.deleteBatchDocument(SETTING_INDEX, PLATFORM_TYPE, idList);
            }
            //保存新的
            ArrayList<DocumentVo> documentVoList = new ArrayList<>();
            for(PlatformVo platformVo : platformVoList) {
                DocumentVo documentVo = new DocumentVo();
                documentVo.setId(platformVo.getName());
                documentVo.setJson(FastJsonConvert.convertObjectToJSON(platformVo));
                documentVoList.add(documentVo);
            }
            elasticSearchUtil.insertBatchDocumentVo(SETTING_INDEX, PLATFORM_TYPE, documentVoList);
        }
    }

    @Override
    public SettingVo loadSetting() {
        SettingVo settingVo = new SettingVo();
        ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("_id", SETTING_TYPE_ID));
        List<SettingVo> settingVoList = elasticSearchUtil.search(SettingVo.class, SETTING_INDEX, SETTING_TYPE, boolQueryBuilder);
        if (settingVoList != null && settingVoList.size() > 0) {
            settingVo = settingVoList.get(0);
        }
        return settingVo;
    }

    @Override
    public void saveSetting(SettingVo settingVo) {
        if(settingVo!=null) {
            ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
            boolean b = elasticSearchUtil.insertOrUpdateDocument(SETTING_INDEX, SETTING_TYPE, SETTING_TYPE_ID, settingVo);
        }
    }

    @Override
    public List<UserVo> loadAllUser() {
        ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        List<UserVo> userVoList = elasticSearchUtil.search(UserVo.class, SETTING_INDEX, USER_TYPE, boolQueryBuilder);
        return userVoList;
    }
    @Override
    public UserVo loadUser(String userName) {
        UserVo userVo = new UserVo();
        ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("_id", userName));
        List<UserVo> userVoList = elasticSearchUtil.search(UserVo.class, SETTING_INDEX, USER_TYPE, boolQueryBuilder);
        if(userVoList!=null){
            userVo= userVoList.get(0);
        }
        return userVo;
    }
    @Override
    public void saveUserList(List<UserVo> userVoList) {
        if(userVoList!=null&&userVoList.size()>0) {
            ElasticSearchUtil elasticSearchUtil = ESFactory.getInstance();
            //删除旧的
            List<UserVo> oldUserVoList = loadAllUser();
            if(oldUserVoList!=null&&oldUserVoList.size()>0) {
                List<String> idList = new ArrayList<>();
                for(UserVo userVo : oldUserVoList) {
                    idList.add(userVo.getUserName());
                }
                elasticSearchUtil.deleteBatchDocument(SETTING_INDEX, USER_TYPE, idList);
            }
            //保存新的
            ArrayList<DocumentVo> documentVoList = new ArrayList<>();
            for(UserVo userVo : userVoList) {
                DocumentVo documentVo = new DocumentVo();
                documentVo.setId(userVo.getUserName());
                documentVo.setJson(FastJsonConvert.convertObjectToJSON(userVo));
                documentVoList.add(documentVo);
            }
            elasticSearchUtil.insertBatchDocumentVo(SETTING_INDEX, USER_TYPE, documentVoList);
        }
    }
}
